package edu.dali.enterprise_test.controller.sale;

import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.service.sale.SaleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("index")
    public String index() {
        return "sale/index";
    }

    @PostMapping("getData")
    @ResponseBody
    public Pager<Dto> getData(int page , int limit) {
        PageHelper.startPage(page, limit);
        Dto dto = new Dto();
        String custname = request.getParameter("custname");
        String dish = request.getParameter("dish");
        if (!StringUtils.isEmpty(custname)) {
            dto.put("custname" , custname);
        }
        if (!StringUtils.isEmpty("dish")) {
            dto.put("dish" , dish);
        }

        Pager<Dto> p = saleService.selectList(dto);
        return p;
    }

}
